//
//  WJHomeTopItem.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/19.
//

import UIKit
import SnapKit // 确保导入 SnapKit

enum HomeTopItemType {
    case homeTopUnsignedItem
    case homeTopSignedItem
}

class WJHomeTopItem: UIControl {

    lazy var signImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.tintColor = .clear
        imageView.image = UIImage(named: "home_top_unsigned")
        return imageView
    }()
    
    lazy var contentImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.tintColor = .clear
        imageView.image = UIImage(named: "signed_icon")
        return imageView
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.font = UIFont.montserratSemiBold(16)
        label.text = "0"
        label.textColor = UIColor(hex: "#121212")
        return label
    }()
    
    var homeTopItemType: HomeTopItemType = .homeTopSignedItem {
        didSet {
            updateSubViews()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(hex: "#121212", alpha: 0.05)
        layer.cornerRadius = 30
        layer.borderColor = UIColor(hex: "#FFFFFF").cgColor
        layer.borderWidth  = 0.5
        layer.shadowColor = UIColor.white.cgColor
        layer.shadowOffset = CGSize(width: 0, height: 0)
        layer.shadowOpacity = 0.7
        layer.shadowRadius = 3.0
        layer.masksToBounds = false
        setupView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupView() {
        addSubview(signImgView)
        addSubview(contentImgView)
        addSubview(titleLabel)
        setViewLayout()
    }
    
    private func setViewLayout() {
        let ratio1 = signImgView.image?.aspectRatioOfWH() ?? 1.0
        signImgView.snp.makeConstraints { make in
            make.leading.trailing.top.equalToSuperview().inset(8)
            make.height.equalTo(signImgView.snp.width).dividedBy(ratio1)
        }
        
        let ratio2 = contentImgView.image?.aspectRatioOfWH() ?? 1.0
        contentImgView.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview().inset(-1)
            make.height.equalTo(contentImgView.snp.width).dividedBy(ratio2)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(40)
            make.bottom.equalToSuperview().offset(-20)
            make.height.equalTo(32)
        }
    }
    
    func updateSubViews() {
        switch homeTopItemType {
        case .homeTopUnsignedItem:
            signImgView.image = UIImage(named: "home_top_unsigned")
            contentImgView.image = UIImage(named: "unsigned_icon")
        case .homeTopSignedItem:
            signImgView.image = UIImage(named: "home_top_signed")
            contentImgView.image = UIImage(named: "signed_icon")
        }
    }
}
